package com.example.drugs1.controllers;

import com.example.drugs1.entities.Room;
import com.example.drugs1.services.DrugsService;
import com.example.drugs1.services.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/rooms")
public class RoomControllers {

    @Autowired
    RoomService roomService;

    @Autowired
    DrugsService drugsService;

    @GetMapping
    public String disRoom(Model model){
        List<Room> rooms =roomService.getALL();
        model.addAttribute("rooms", rooms);
        Authentication authentication= SecurityContextHolder.getContext().getAuthentication();
        if ("anonymousUser".equals(authentication.getName())){
            model.addAttribute("name","未登录");
        }else {
            model.addAttribute("name",authentication.getName());
        }
        return "rooms/list-room";
    }

    @GetMapping("/new")
    public String displayRoomFrom(Model model){
        Room aroom=new Room();
        model.addAttribute("aroom",aroom);
        Authentication authentication=SecurityContextHolder.getContext().getAuthentication();
        if ("anonymousUser".equals(authentication.getName())){
            model.addAttribute("name","未登录");
        }else {
            model.addAttribute("name",authentication.getName());
        }
        return "rooms/new-room";
    }

    @PostMapping("/save")
    public String createRoom(Model model, @Valid Room room,@RequestParam("name")String name){
            if (name.equals("")) {
                model.addAttribute("error", "注意：名称不能为空");
                return "rooms/new-room";
            }else {
            roomService.save(room);
        return "redirect:/rooms";}
    }


    @GetMapping("/delete")
    public String deleteRoom(@RequestParam("id")long theId, Model model){
        Room theBallteam = roomService.findByRoomId(theId);
        roomService.delete(theBallteam);
        return "redirect:/rooms";
    }

    @RequestMapping("/selects")

    public String selects(Model model){
        Authentication authentication=SecurityContextHolder.getContext().getAuthentication();
        if ("anonymousUser".equals(authentication.getName())){
            model.addAttribute("name","未登录");
        }else {
            model.addAttribute("name",authentication.getName());
        }
        return "rooms/select-rooms";
    }

    @PostMapping("/select")
    public String selectRoom(@RequestParam("name") String theName,Model model){
        List<Room> Sroom =roomService.selectRoom(theName);
        model.addAttribute("sroom", Sroom);
        if(Sroom.size()<1){
            model.addAttribute("error","查询结果为空，请重新输入");
        }
        return "rooms/select-rooms";
    }
}
